<?php


namespace App\Http\Controllers\Api;


use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;

class UserController extends ApiBaseController
{
    /**
     * 注册
     * @bodyParam name string required 用户名
     * @bodyParam register_type string required 注册类型（1邮箱 2手机）.
     * @bodyParam password string required 密码
     * @bodyParam invite_code string options 邀请码
     * @param Request $request
     * @param User $user
     * @return \Illuminate\Http\JsonResponse
     */
    public function register(Request $request, User $user)
    {
        $data = $request->all();
        if ($vr = $this->verifyField($data, [
            // 'ip', 'invite_code', 'name', 'register_type', 'password', 'device_id', 'area_id', 'verify_code'
            'invite_code' => '',
            'name' => 'bail|required|email|min:8|max:64',
            'password' => 'bail|required|min:6|max:32',
            'verify_code' => '',
        ])) return $vr;

        $data = array_merge($data, $this->getHeaders($request));
        $result = $user->register($data);
        return $this->success($result);
    }

    public function login(Request $request, User $user)
    {
        $data = $request->all();
        $data = array_merge($data, $this->getHeaders($request));
        $result = $user->login($data);
        return $this->success($result);
    }

    public function getUser(Request $request, User $user)
    {
        $data = $request->all();
        $data['uid'] = $this->getUid($request);
        $result = $user->getUser($data);
        return $this->success($result);
    }

    public function updatePwd(Request $request, User $user)
    {
        $data = $request->all();
        if ($vr = $this->verifyField($data, [
            'old_pwd' => 'bail|required|alpha_num|min:6|max:32',
            'new_pwd' => 'bail|required|alpha_num|min:6|max:32',
        ])) return $vr;

        $data['uid'] = $this->getUid($request);
        $result = $user->updatePwd($data);
        return $this->success($result);
    }

    public function updatePayPwd(Request $request, User $user)
    {
        $data = $request->all();
        if ($vr = $this->verifyField($data, [
            'old_pwd' => 'bail|required|alpha_num|min:6|max:32',
            'new_pwd' => 'bail|required|alpha_num|min:6|max:32',
        ])) return $vr;
        $data['uid'] = $this->getUid($request);
        $result = $user->updatePayPwd($data);
        return $this->success($result);
    }

    public function resetPwd(Request $request, User $user)
    {
        $data = $request->all();
        if ($vr = $this->verifyField($data, [
            'password' => 'bail|required|alpha_num|min:6|max:32',
            'device_id' => 'bail|required',
            'verify_code' => 'bail|required|digits:6',
            'register_type' => 'bail|in:1,2|required',
            'name' => 'bail|required',
        ])) return $vr;
        $data['uid'] = $this->getUid($request);
        $result = $user->resetPwd($data);
        return $this->success($result);
    }

    public function resetPayPwd(Request $request, User $user)
    {
        $data = $request->all();
        if ($vr = $this->verifyField($data, [
            'password' => 'bail|required|alpha_num|min:6|max:32',
            'verify_code' => 'bail|required|digits:6',
            'name' => 'bail|required',
        ])) return $vr;

        $data['uid'] = $this->getUid($request);
        $result = $user->resetPayPwd($data);
        return $this->success($result);
    }

    public function profile(Request $request, User $user)
    {
        $uid = $this->getUid($request);
        return $this->success(
            $user->userCenterProfile($uid)
        );
    }
}
